package com.dcy.znoa.service.attendance.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.DateTimeUtil;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.attendance.model.attRealRecord.*;
import com.dcy.znoa.dao.attendance.ApplyDao;
import com.dcy.znoa.dao.attendance.AttRealRecordDao;
import com.dcy.znoa.dao.attendance.AttRecordDao;
import com.dcy.znoa.dao.sys.AppVersionDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.attendance.Apply;
import com.dcy.znoa.domain.attendance.ArrAndApplyVo;
import com.dcy.znoa.domain.attendance.AttRealRecord;
import com.dcy.znoa.domain.sys.AppVersion;
import com.dcy.znoa.domain.sys.Role;
import com.dcy.znoa.service.attendance.AttRealRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class AttRealRecordServiceImpl implements AttRealRecordService {
    @Autowired
    AttRecordDao attRecordDao;
    @Autowired
    AttRealRecordDao attRealRecordDao;
    @Autowired
    OperDao operDao;
    @Autowired
    ApplyDao applyDao;
    @Autowired
    AppVersionDao appVersionDao;

    @Override
    public List selectList() throws InvocationTargetException, IllegalAccessException {
        return null;
    }

    @Override
    public Integer insert(Role role) {
        return null;
    }

    @Override
    public Integer updateById(Role role) {
        return null;
    }

    @Override
    public List queryReasonList(SelectAbnormalARRModel model) {
        return attRealRecordDao.queryReasonList(model);
    }

    @Override
    public List queryList(SelectARRModel model) throws InvocationTargetException, IllegalAccessException {
        EntityWrapper ew = new EntityWrapper<AttRealRecord>();
        AttRealRecord cp = new AttRealRecord();
        BeanUtils.copyProperties(cp, model);
        ew.setEntity(cp);
        List list = attRealRecordDao.selectList(ew);
        return list;

//        List list = attRealRecordDao.selectListMapper(new EntityWrapper<AttRealRecord>()
//                .eq("record_id",model.getRecordId())
//                .eq("oper_id",model.getOperId())
//                .eq("comp_id",model.getCompId())
//                .eq("record_type",model.getRecordType())
//                .eq("time_status",model.getTimeStatus())
//                .eq("add_status",model.getAddStatus())
//                .eq("att_time",model.getAttTime())
//        );
//        return attRealRecordDao.selectArrAndApply(model);
    }

    @Override
    public List queryGroup(SelectAbnormalARRModel model) {
        return attRealRecordDao.queryGroup(model);
    }

    @Override
    public List queryTimesByAttTypeAndOper(SelectTimebyAttTypeModel model) throws BusinessException {
        EntityWrapper ew = new EntityWrapper<AttRealRecord>();
        ew.eq("oper_id", model.getOperId());
        String st = model.getSt();
        String et = model.getEt();
        if (StringUtils.isNotBlank(st)) {
            ew.ge("att_time", st);
        }
        if (StringUtils.isNotBlank(et)) {
            ew.le("att_time", et);
        }

//        ew.eq("operId", model.getOperId())
//                .ge("attTime", model.getSt())
//                .le("attTime", model.getEt());
        Integer attType = model.getAttType();
        if (attType == null) {
            throw new BusinessException("404", "入参考勤类型缺失");
        }
        switch (attType) {//0  正常 1 迟到  2 早退 3 未打卡 4 地点异常 ，这个和数据库中略有不同
            case 0:
//                ew.eq("time_status", 0).or("add_status = 0");
//                ew.last("and (time_status = 0 or add_status =0)");
                ew.last("and time_status = 0 and add_status =0 ");
                break;
            case 1:
                ew.eq("record_type", 1).eq("time_status", 8);
                break;
            case 2:
                ew.eq("record_type", 2).eq("time_status", 8);
                break;
            case 3:
                ew.eq("add_status", 9).eq("time_status", 9);
                break;
            case 4:
                ew.eq("add_status", 8);
                break;
            default:
                throw new BusinessException("404", "入参考勤类型有误");
        }
        return attRealRecordDao.selectList(ew);
//        return attRealRecordDao.queryTimesByTypeAndOper(model);
    }

    ;

    @Override
    public List queryTimesByApplyTypeAndOper(SelectTimebyApplyTypeModel model) throws BusinessException {
        EntityWrapper ew = new EntityWrapper<Apply>();
        Apply apply = new Apply();
        apply.setCompId(1L);
        ew.setEntity(apply);
        ew.eq("send_oper_id", model.getOperId());
        String st = model.getSt();
        String et = model.getEt();
        if (StringUtils.isNotBlank(st)) {
            ew.ge("start_time", st);
        }
        if (StringUtils.isNotBlank(et)) {
            ew.le("end_time", et);
        }

        Integer attType = model.getApplyType();
        switch (attType) {////1 请假 2 出差 3 外勤 9申述 ，这个和数据库中一样
            case 1:
                ew.eq("type", 1);
                break;
            case 2:
                ew.eq("type", 2);
                break;
            case 3:
                ew.eq("type", 3);
                break;
            case 9:
                ew.eq("type", 9);
                break;
            default:
                throw new BusinessException("404", "入参申请类型有误");
        }
        return applyDao.selectList(ew);
//        return applyDao.selectListMapper(new EntityWrapper<Apply>());
//        return attRealRecordDao.queryTimesByApplyTypeAndOper(model);

//        EntityWrapper ew = new EntityWrapper<Apply>();
//        ew.setEntity(new Apply());
//        List list = applyDao.selectList(ew);
//        return list;
    }

    @Override
    public List queryOpersByAttType(QueryOpersByAttTypeModel model) throws BusinessException {
        Integer attType = model.getAttType();
        switch (attType) {//0  正常 1 迟到  2 早退 3 未打卡 4 地点异常 ，这个和数据库中略有不同
            case 0:
                return attRealRecordDao.queryOpersByAttType0(model);
            case 1:
                return attRealRecordDao.queryOpersByAttType1(model);
            case 2:
                return attRealRecordDao.queryOpersByAttType2(model);
            case 3:
                return attRealRecordDao.queryOpersByAttType3(model);
            case 4:
                return attRealRecordDao.queryOpersByAttType4(model);
            default:
                throw new BusinessException("404", "入参考勤类型有误");
        }
//        return applyDao.queryOpersByAttType(model);
    }

    @Override
    public List queryOpersByApplyType(QueryOpersByApplyTypeModel model) throws BusinessException {

//        Integer attType = model.getApplyType();
//        switch (attType){//0  正常 1 迟到  2 早退 3 未打卡 4 地点异常 ，这个和数据库中略有不同
//            case 1:
//                return attRealRecordDao.queryOpersByApplyType(model);
//            case 2:
//                return attRealRecordDao.queryOpersByApplyType1(model);
//            case 3:
//                return attRealRecordDao.queryOpersByApplyType3(model);
////            case 9:
////                return attRealRecordDao.queryOpersByApplyType3(model);
//            default:
//                throw new BusinessException("404","入参考勤类型有误");
//        }
//        return null;
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List list = applyDao.queryOpersByApplyType(model, page);
        return list;
    }

    @Override
    public JsonForList queryAttAndApplyGroup(QueryAttAndApplyModel model) {
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List<ArrAndApplyVo>  list1 = attRealRecordDao.queryAttAndApplyGroup1(model, page);
//        log.info(String.valueOf(list1.size()));
        List<ArrAndApplyVo>  list2 = attRealRecordDao.queryAttAndApplyGroup2(model);
//        log.info(String.valueOf(list2.size()));
//        for(int i=0;i<list1.size();i++){
////            log.info(list1.get(i).toString());
////            log.info(list2.get(i).toString());
//            list1.get(i).setAppLeave("0");
//            list1.get(i).setAppOut("0");
//            list1.get(i).setAppTravel("0");
//            for (int j =0;j<list2.size();j++){
//                if(list1.get(i).getOperId().equals(list2.get(j).getOperId())){
//                    list1.get(i).setAppLeave(list2.get(j).getAppLeave());
//                    list1.get(i).setAppOut(list2.get(j).getAppOut());
//                    list1.get(i).setAppTravel(list2.get(j).getAppTravel());
//                    break;
//                }
//            }
//        }
        for(ArrAndApplyVo arrAndApplyVo1: list1){
//            log.info(list1.get(i).toString());
//            log.info(list2.get(i).toString());
            arrAndApplyVo1.setAppLeave("0");
            arrAndApplyVo1.setAppOut("0");
            arrAndApplyVo1.setAppTravel("0");
            for(ArrAndApplyVo arrAndApplyVo2: list2){
                if(arrAndApplyVo1.getOperId().equals(arrAndApplyVo2.getOperId())){
                    arrAndApplyVo1.setAppLeave(arrAndApplyVo2.getAppLeave());
                    arrAndApplyVo1.setAppOut(arrAndApplyVo2.getAppOut());
                    arrAndApplyVo1.setAppTravel(arrAndApplyVo2.getAppTravel());
                    break;
                }
            }
        }
        return new JsonForList(list1, page.getTotal());
//        return list1;
//        return attRealRecordDao.queryAttAndApplyGroup(model);
    }

    @Override
    public JsonForList queryAttDetailAllDay(QueryAttAndApplyModel model) {
        Page page = new Page(model.getPageNum(), model.getPageSize());
        List list = attRealRecordDao.queryAttDetailAllDay(model,page);
        return new JsonForList(list, page.getTotal());
    }

    @Override
    public List queryOneDayApply(String dateStr,Long operId) {
        Date date = DateTimeUtil.StrDateToDate(dateStr);
        EntityWrapper ewArr = new EntityWrapper();
        AttRealRecord arr = new AttRealRecord();
        arr.setAttTime(date);
        arr.setOperId(operId);
        ewArr.setEntity(arr);
        List<AttRealRecord> attRealRecordList = attRealRecordDao.selectList(ewArr);
        for(AttRealRecord attRealRecord : attRealRecordList){
            Apply apply = new Apply();
            apply.setAttTime(date);
            EntityWrapper ewApply = new EntityWrapper();
            ewApply.setEntity(apply);
            ewApply.eq("e_id",attRealRecord.getRecordId());
            ewApply.eq("e_name","att_real_record");
            ewApply.eq("send_oper_id",operId);
//            ewApply.ge("start_time",dateStr+" 00:00:00");
//            ewApply.le("end_time",dateStr+" 23:59:59");
            List<Apply> applyList = applyDao.selectList(ewApply);
//            attRealRecord.setApplyList(applyList);
            if(!applyList.isEmpty()){
                attRealRecord.setApply(applyList.get(0));
            }
        }
        return attRealRecordList;
    }
}
